﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Sunboxsoft.ASMS.Client.UI.Properties;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
namespace Sunboxsoft.ASMS.Client.UI.Create
{
    /// <summary>
    /// 创建库房
    /// </summary>
    public partial class CreateWarehouseView : UserControl, IView
    {
        Promise _promise = new Promise();

        public CreateWarehouseView()
        {
            InitializeComponent();
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(this.txtCode, "仓库编码", false));
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(this.txtName, "仓库名称", false));
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(this.txtAddress, "详细地址", true));
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(this.txtContactName, "联系人姓名", true));
            ValidationManager.Register(this, new Common.Validations.TextEditTelephoneValidation(this.txtTel, true));
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(this.txtDes, "描述", true));
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("save", Resources.isOK, "保存", btnSave_Click))
                .AddAction(new ActionNode("cancel", Resources.isBack, "取消", btnCancel_Click));
        }

        public void Active(ViewActiveArgs args)
        {
            _promise.Then(() =>
            {
                SearchLookUpEditUtil.BindEMasterDataItem(this.ctlLookUpCountry,
                    _promise,
                    Domain.Constant.MasterDataCodes.Country,
                    null, null, true, true);
            }).Run();
        }

        public void Resume(ViewResumeArgs args)
        {
        }

        public ViewHeader GetHeader()
        {
            return new ViewHeader("创建库房");
        }

        #endregion

        #region Mehtod
        /// <summary>
        /// 新建库房
        /// </summary>
        public void CreateWareHouse()
        {
            WebRef.EWarehouse model = new WebRef.EWarehouse();

            model.BusinessId = ModuleContext.Current.BusinessId;
            model.StationId = ModuleContext.Current.StationId;
            model.Code = this.txtCode.Text.Trim();
            model.Name = this.txtName.Text.Trim();
            model.Country = SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpCountry).GetCode();
            model.Province = SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpProvince).GetCode();
            model.City = SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpCity).GetCode();
            model.District = SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpDistrict).GetCode();
            model.Address = this.txtAddress.Text.Trim();
            model.ContactName = this.txtContactName.Text.Trim();
            model.ContactTelephone = this.txtTel.Text.Trim();
            model.Description = this.txtDes.Text.Trim();

            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                ServiceResult result = new Service.CreateService().CreateWarehouse(model);
                this.BeginInvoke(new Action(() =>
                {
                    this.HideMask();
                    if (result.IsSuccess)
                    {
                        UIContext.Current.ViewManager.CloseView(true, null);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                }));
            });
        }
        #endregion

        #region Action
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, object e)
        {
            string strMess = null;
            if (ValidationManager.Validate(this, out strMess))
            {
                CreateWareHouse();
            }
            else
            {
                UIContext.Current.ViewManager.ShowWarning(strMess);
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        /// <summary>
        /// 根据选择国家获取省份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lupCountry_EditValueChanged(object sender, EventArgs e)
        {
            SearchLookUpEditUtil.BindEMasterDataItem(this.ctlLookUpProvince,
                _promise,
                Domain.Constant.MasterDataCodes.Province,
                SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpCountry).GetCode()
                , null, true, true);
        }
        /// <summary>
        /// 根据选择省获取市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lupProvince_EditValueChanged(object sender, EventArgs e)
        {
            SearchLookUpEditUtil.BindEMasterDataItem(this.ctlLookUpCity,
                _promise,
                Domain.Constant.MasterDataCodes.City,
                 SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpProvince).GetCode()
                , null, true, true);
        }
        /// <summary>
        /// 根据市获取区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lupCity_EditValueChanged(object sender, EventArgs e)
        {
            if (this.ctlLookUpCity.EditValue == null)
                return;

            SearchLookUpEditUtil.BindEMasterDataItem(this.ctlLookUpDistrict,
                _promise,
                Domain.Constant.MasterDataCodes.District,
                  SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlLookUpCity).GetCode()
                , null, true, true);
        }
        #endregion


    }
}
